package com.rpc.netty.httpNetty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc 接收信息和发送信息
 */

public class HTTPServerHandler extends SimpleChannelInboundHandler<HttpObject> {

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    //判断是否是HTTPRequest请求

    if (msg instanceof HttpRequest) {

      ByteBuf content = Unpooled.copiedBuffer("hello 我是服务器", CharsetUtil.UTF_8);

      DefaultFullHttpResponse response = new DefaultFullHttpResponse(
          HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);

      response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
      response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());

      ctx.writeAndFlush(response);



    }

  }
}
